# sudo apt-get install wmctrl
import subprocess
import time
import pyautogui
import random


# # positon()获取鼠标位置，返回二元元组
# positon = pyautogui.position()
# position_x, position_y = pyautogui.position()
#
# # moveTo()，参数可以为x, y，也可以是元组(x, y)，列表[x, y]
# pyautogui.moveTo(x, y)	# 移动至(x, y)
# pyautogui.moveTo(position)				# 花零秒时间移动
# pyautogui.moveTo(position, duration=1)	# 花一秒时间移动
#
# # moveRel() 参数同上，表示相对位移
# pyautogui.moveRel(100, -100) # 向右移动100， 向上移动100
#
# # click()
# pyautogui.click() #点击当前位置，也可以传入位置
# pyautogui.click(100, 100, button='left', duration=1)	# 花一秒移动到(100,100) ，左键点击
#
# # dragTo()
# pyautogui.dragTo(position, duration=1)	# 花一秒按下鼠标移动到(100,100)
#
# # mouseDown()	# 鼠标按下
# # mouseUp()
# mouseDown()
# moveTo(100, 100, duration=1)
# mouseUp()
# # 等效于
# dragTo(100, 100, duration=1)
#
#

class FetchTools(object):
    def __init__(self):
        self.name = 'web fetch tools'

    @staticmethod
    def get_current_pos():
        # positon()获取鼠标位置，返回二元元组
        positon = pyautogui.position()
        position_x, position_y = pyautogui.position()
        return position_x, position_y

    @staticmethod
    def get_window_ids_by_title(title_fragment):
        # 运行 wmctrl -l 命令并获取输出
        result = subprocess.run(['wmctrl', '-l'], capture_output=True, text=True)
        lines = result.stdout.strip().split('\n')

        window_ids = []
        for line in lines:
            parts = line.split()
            print(parts)
            if title_fragment.lower() in parts[-1].lower():  # 检查窗口标题是否包含指定的片段
                window_id = parts[0]  # 第一个元素是窗口 ID
                window_ids.append(window_id)

        return window_ids

    @staticmethod
    def put_window_to_frout(window_id):
        subprocess.run(['wmctrl', '-a', window_id])

    @staticmethod
    def move_to(position):
        pyautogui.moveTo(position, duration=random.randint(1,5)+random.random())  # 花一秒时间移动

    @staticmethod
    def click():
        pyautogui.click()  # 点击当前位置，也可以传入位置

    @staticmethod
    def ctrl_a():
        pyautogui.hotkey('ctrl', 'a')

    @staticmethod
    def ctrl_c():
        pyautogui.hotkey('ctrl', 'c')

    @staticmethod
    def ctrl_v():
        pyautogui.hotkey('ctrl', 'v')


first = True
fetch_tools = FetchTools()
# # 1、切换到chrome
# # window_ids = fetch_tools.get_window_ids_by_title("Chrome")
# fetch_tools.put_window_to_frout("Google Chrome")
# # 2、crtl+a   ctrl+c 复制
# fetch_tools.ctrl_a()
# fetch_tools.ctrl_c()
# time.sleep(random.random() * 5)
# # 3、 切换到文本编辑器
# fetch_tools.put_window_to_frout("无标题文档")
# # fetch_tools.put_window_to_frout("无标题文档")
# # 4、粘贴
# fetch_tools.ctrl_v()

# for i in range(0,100):
#     time.sleep(1)
#     print(fetch_tools.get_current_pos())

for i in range(0, 983):
    # 5、切换到chrome
    fetch_tools.put_window_to_frout("Google Chrome")
    time.sleep(random.randint(3,5)+random.random())
    if i == 1:
        # 6、点击下一个20
        fetch_tools.move_to((518, 319))
        fetch_tools.click()
        time.sleep(random.randint(12,14)+random.random())
    elif i > 1:
        fetch_tools.move_to((601, 325))
        fetch_tools.click()
        time.sleep(random.randint(12,14)+random.random())
    time.sleep(random.randint(1,2)+random.random())
    fetch_tools.ctrl_a()
    fetch_tools.ctrl_c()
    fetch_tools.put_window_to_frout("主要新闻")
    time.sleep(random.randint(1,2)+random.random())
    # 4、粘贴
    fetch_tools.ctrl_v()
    first = False

"""
cat 1.txt |grep -v 'BBCM Ukraine Watchlist 22 Nov 24'|grep -v -i summary|grep -v 'Nov 24'|grep 'Factiva 授权资讯内容' |sort |uniq|sort|wc -l
"""